package com.dc.web.controller;

import com.dc.constant.Constants;
import com.dc.dao.ShipDao;
import com.dc.dao.ShipRepairDao;
import com.dc.dao.WorkflowTopFirstDao;
import com.dc.dao.vo.ShipNameAndRepairIdVO;
import com.dc.dao.vo.WorkflowChartVO;
import com.dc.entity.ShipRepairEntity;
import com.dc.exception.ServiceException;
import com.dc.model.AjaxResult;
import com.dc.model.WorkflowEnergyChartBody;
import com.dc.model.WorkflowShipChartBody;
import com.dc.service.ShipRepairService;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;


/**
 * 单船相关柱状图
 */
@Api(tags = "单船相关柱状图")
@RequestMapping("/api/workflow_chart")
@RestController
public class WorkflowChartController {

    @Resource
    private ShipRepairDao shipRepairDao;

    @Resource
    private ShipRepairService shipRepairService;

    @Resource
    private WorkflowTopFirstDao workflowTopFirstDao;

    //SELECT ship_repair_id, sum(c_shen) as c_shen_sum, sum(c_ocean) as c_ocean_sum FROM t_workflow_stat_first GROUP BY ship_repair_id ORDER BY c_shen_sum;

    //SELECT ship_repair_id, energy_category, energy_amount FROM t_workflow_stat_first WHERE energy_category = "电力" ORDER BY energy_amount ;

    // 单船的 top 10 碳排放图
    @GetMapping("/ship")
    public AjaxResult ship(WorkflowShipChartBody body) {

        Date beginTime = body.getBeginTime();   // TODO 开始时间，结束时间为空 如何处理 ？？？

        Date endTime = body.getEndTime();

        List<ShipRepairEntity> shipRepairEntityList = null;

        if(ObjectUtils.isEmpty(beginTime) && ObjectUtils.isEmpty(endTime)) {
            shipRepairEntityList = shipRepairDao.findByComplete(Constants.SHIP_REPAIR_STAT_COMPLETE);
        } else if(ObjectUtils.isNotEmpty(beginTime) && ObjectUtils.isNotEmpty(endTime)) {
            shipRepairEntityList = shipRepairDao.findByOutTimeBetweenAndComplete(beginTime, endTime, Constants.SHIP_REPAIR_STAT_COMPLETE);
        } else {
            throw new ServiceException("时间不可以只选择一个，请全部选择，或者全部留空");
        }

        List<Long> collect = shipRepairEntityList.stream().map(ShipRepairEntity::getId).collect(Collectors.toList());

        List<WorkflowChartVO> chartVOS ;

        Integer cType = body.getCType();
        if(Constants.TYPE_C_SHENZHEN.equals(cType)) {
            chartVOS = workflowTopFirstDao.findShipShenChart(collect);
        } else if(Constants.TYPE_C_OCEAN.equals(cType)) {
            chartVOS = workflowTopFirstDao.findShipOceanChart(collect);
        } else {
            throw new ServiceException("cType 参数错误");
        }

        Map<Long, String> shipNameRepairIdMap = shipRepairService.getShipNameRepairIdMap();

        List<Map<String, Object>> result = new ArrayList<>();

        for(WorkflowChartVO chartVO : chartVOS) {
            Map<String, Object> tmp = new HashMap<>();
            String shipName = shipNameRepairIdMap.getOrDefault(chartVO.getShipRepairId(), "");
            Double value = chartVO.getValue();
            tmp.put("shipName", shipName);
            tmp.put("value", value);

            result.add(tmp);
        }

        return AjaxResult.success(result);
    }

    // 单船能源 top 10 图
    // 电力 柴油 液化天然气 二氧化碳
    @GetMapping("/energy")
    public AjaxResult energy(WorkflowEnergyChartBody body) {

        Date beginTime = body.getBeginTime();   // TODO 开始时间，结束时间为空 如何处理 ？？？

        Date endTime = body.getEndTime();

        List<ShipRepairEntity> shipRepairEntityList = null;

        if(ObjectUtils.isEmpty(beginTime) && ObjectUtils.isEmpty(endTime)) {
            shipRepairEntityList = shipRepairDao.findByComplete(Constants.SHIP_REPAIR_STAT_COMPLETE);
        } else if(ObjectUtils.isNotEmpty(beginTime) && ObjectUtils.isNotEmpty(endTime)) {
            shipRepairEntityList = shipRepairDao.findByOutTimeBetweenAndComplete(beginTime, endTime, Constants.SHIP_REPAIR_STAT_COMPLETE);
        } else {
            throw new ServiceException("时间不可以只选择一个，请全部选择，或者全部留空");
        }

        List<Long> collect = shipRepairEntityList.stream().map(ShipRepairEntity::getId).collect(Collectors.toList());

        List<WorkflowChartVO> chartVOS ;

        Integer cType = body.getCType();
        String energyCategory = body.getEnergyCategory();
        if(Constants.TYPE_C_SHENZHEN.equals(cType)) {
            chartVOS = workflowTopFirstDao.findEnergyShenChart(collect, energyCategory);
        } else if(Constants.TYPE_C_OCEAN.equals(cType)) {
            chartVOS = workflowTopFirstDao.findEnergyOceanChart(collect, energyCategory);
        } else {
            throw new ServiceException("cType 参数错误");
        }

        Map<Long, String> shipNameRepairIdMap = shipRepairService.getShipNameRepairIdMap();

        List<Map<String, Object>> result = new ArrayList<>();

        for(WorkflowChartVO chartVO : chartVOS) {
            Map<String, Object> tmp = new HashMap<>();
            String shipName = shipNameRepairIdMap.getOrDefault(chartVO.getShipRepairId(), "");
            Double value = chartVO.getValue();
            tmp.put("shipName", shipName);
            tmp.put("value", value);

            result.add(tmp);
        }

        return AjaxResult.success(result);
    }
}
